package goodbaby.dklt.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.GbbPreferences;
import goodbaby.dklt.R;
import goodbaby.dklt.activity.LoginActivity;
import goodbaby.dklt.bean.SportInfo;
import goodbaby.dklt.customerview.ColorArcProgressBar;
import goodbaby.dklt.customerview.HistoryFormView;
import goodbaby.dklt.customerview.Top5ListView;

/**
 * Created by Li.z on 2016/4/29.
 * 步数
 */
public class StepNumberFragment extends HealthBaseFragment {
    public static final String TAG = "StepNumberFragment";

    private ColorArcProgressBar mProgressBar;
    private TextView tvCalorie;
    private TextView tvPercent;
    private TextView tvStepNumber;
    private TextView tvPostion;
    private TextView tvTarget;

    private Top5ListView listview;
    private HistoryFormView historyFormView;

    public static boolean shouldLoadData = true;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_step_number;
    }

    @Override
    public void initView(View parentView) {
        super.initView(parentView);

        mProgressBar = (ColorArcProgressBar) parentView.findViewById(R.id.cap_progressbar);


        tvCalorie = (TextView) parentView.findViewById(R.id.tv_calorie);
        tvPercent = (TextView) parentView.findViewById(R.id.tv_percent);
        tvStepNumber = (TextView) parentView.findViewById(R.id.tv_step_number);
        tvPostion = (TextView) parentView.findViewById(R.id.tv_position);
        tvTarget = (TextView) parentView.findViewById(R.id.tv_target);

        listview = (Top5ListView) parentView.findViewById(R.id.tlv_top_list);
        historyFormView = (HistoryFormView) parentView.findViewById(R.id.hfv_step_number);

       /* loadData();
        Log.i(TAG,"------*****");*/
    }

    public void loadData() {
        ActionHelp.getSportInfo(getActivity(), new ObjectCallback<SportInfo>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onSuccess(SportInfo response) {
                Log.i(TAG, response.toString());
                showView(response);
            }

            @Override
            public Type getType() {
                return new TypeToken<SportInfo>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                ToastUtil.showToast(getActivity(), "请求错误");
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(getActivity(), "请求超时");
            }
        });
    }

    private float maxAngle = 300;
    private float startAngle = 120f;

    private void showView(SportInfo info) {
        int sObject = Integer.parseInt(GbbPreferences.getSObject());
        tvCalorie.setText(info.getTakeEnergy() + "Cal");
        tvTarget.setText("目标" + sObject + "步");
        tvPostion.setText(TextUtils.isEmpty(info.getRanking()) ? 0 + "" : info.getRanking());
        tvPercent.setText(info.getSRange() + "M");
//        try {
//            tvPercent.setText(String.format("%.1f", Float.parseFloat(info.getFinishValue())) + "%");
//        } catch (Exception e) {
//            tvPercent.setText(info.getFinishValue());
//        }


        final float step = info.getSportStep();

        final float target = sObject;

        float sweep = maxAngle * (step / target);
        if (sweep > maxAngle) {
            sweep = maxAngle;
        } else if (sweep <= 0) {
            sweep = 0;
        }

        if (sweep != 0) {
            mProgressBar.setOnSweepListener(new ColorArcProgressBar.OnSweepListener() {
                @Override
                public void onSweep(float startAngle, float currAngle, float endAngle) {
                    if (step > target) {
                        tvStepNumber.setText((int) step + "");
                    } else {
                        tvStepNumber.setText((int) (target * (currAngle / maxAngle)) + "");
                    }
                }
            });
            mProgressBar.sweepAnimation(startAngle, sweep);
        }

        showHistory(info.getSportsHistoryList());

        showTop5list(info.getSportsTop5List());

    }

    private void showTop5list(final List<SportInfo.Top5User> list) {
        listview.setTitle("步数排行榜");
        listview.setDatas(new Top5ListView.DataAdapter() {
            @Override
            public List<Top5ListView.Top5Data> getData() {
                if (list == null)
                    return null;
                SportInfo.Top5User tempUser;
                List<Top5ListView.Top5Data> datas = new ArrayList<Top5ListView.Top5Data>(list.size());
                for (int i = 0; i < list.size() && i < 5; i++) {
                    tempUser = list.get(i);
                    //datas.add(new Top5Data("http://img2.imgtn.bdimg.com/it/u=4274371413,448906275&fm=23&gp=0.jpg","username"+i,"2000"));
                    datas.add(new Top5ListView.Top5Data(tempUser.getImagePath(), tempUser.getChildrenName(), tempUser.getSportStep() + ""));
                }
                return datas;
            }
        });
    }

    private void showHistory(final List<SportInfo.SportsHistory> list) {
        historyFormView.setLeftString("最近7天");

        if (list == null)
            return;

        historyFormView.setHistoryData(new HistoryFormView.DataAdapter() {
            @Override
            public List<HistoryFormView.HistoryData> getData() {
                List<HistoryFormView.HistoryData> datas = new ArrayList<HistoryFormView.HistoryData>(list.size());

                int max = 0;
                int total = 0;
                for (int i = 0; i < list.size() && i < 7; i++) {
                    total += list.get(i).getSportStep();
                    if (max < list.get(i).getSportStep()) {
                        max = list.get(i).getSportStep();
                    }
                }

                historyFormView.setRightString("平均" + total / 7 + "步/天");

                for (int i = 0; i < list.size() && i < 7; i++) {
                    HistoryFormView.HistoryData data = new HistoryFormView.HistoryData();
                    data.number = (int) (8 * (list.get(i).getSportStep() / (float) max));
                    data.txt = HistoryFormView.getToday(list.get(i).getCreatedOn());
                    datas.add(data);
                }

                return datas;
            }
        });

        historyFormView.show();
    }

   /* @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(isVisibleToUser && shouldLoadData){
            shouldLoadData = false;
            loadData();
        }
    }*/
}
